/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.modules.sys.entity.Dict;
import com.eit.eitsite.modules.sys.utils.DictUtils;
import com.zhuoyue.zhtj.entity.Classroominfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Dispatchclassroom;
import com.zhuoyue.zhtj.service.DispatchclassroomService;

import java.util.List;
import java.util.Map;

/**
 * 考勤机分布Controller
 * @author hss
 * @version 2017-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/dispatchclassroom")
public class DispatchclassroomController extends BaseController {

	@Autowired
	private DispatchclassroomService dispatchclassroomService;
	
	@ModelAttribute
	public Dispatchclassroom get(@RequestParam(required=false) String id) {
		Dispatchclassroom entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dispatchclassroomService.get(id);
		}
		if (entity == null){
			entity = new Dispatchclassroom();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:dispatchclassroom:view")
	@RequestMapping(value = {"list", ""})
	public String list(Dispatchclassroom dispatchclassroom, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Dispatchclassroom> page = dispatchclassroomService.findPage(new Page<Dispatchclassroom>(request, response), dispatchclassroom);
		List<Dispatchclassroom> list=page.getList();
		if(list!=null&&list.size()>0){
			List<Classroominfo> lists=dispatchclassroomService.selectClassroomInfo(list);
			if(lists!=null){
				model.addAttribute("lists", lists);
			}
		}
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/dispatchclassroomList";
	}

	@RequiresPermissions("zhtj:dispatchclassroom:view")
	@RequestMapping(value = "form")
	public String form(Dispatchclassroom dispatchclassroom, Model model) {
		Map<String,List<Classroominfo>> map=dispatchclassroomService.getDic();
		model.addAttribute("dispatchclassroom", dispatchclassroom);
		model.addAttribute("map", map);
		return "zhuoyue/zhtj/dispatchclassroomForm";
	}

	@RequiresPermissions("zhtj:dispatchclassroom:edit")
	@RequestMapping(value = "save")
	public String save(Dispatchclassroom dispatchclassroom, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dispatchclassroom)){
			return form(dispatchclassroom, model);
		}
		dispatchclassroomService.save(dispatchclassroom);
		addMessage(redirectAttributes, "保存考勤机分布成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/dispatchclassroom/?repage";
	}
	
	@RequiresPermissions("zhtj:dispatchclassroom:edit")
	@RequestMapping(value = "delete")
	public String delete(Dispatchclassroom dispatchclassroom, RedirectAttributes redirectAttributes) {
		dispatchclassroomService.delete(dispatchclassroom);
		addMessage(redirectAttributes, "删除考勤机分布成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/dispatchclassroom/?repage";
	}

}